﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using SYH178Utils;

namespace SYH178DAL
{
    public class Purchase_Info
    {
        public Purchase_Info()
        { }
        #region  BasicMethod

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(SYH178Model.Purchase_Info model)
        {

            SqlParameter[] parameters = {
					new SqlParameter("@Title", SqlDbType.NVarChar,200),
					new SqlParameter("@PurchaseName", SqlDbType.NVarChar,50),
					new SqlParameter("@Standard", SqlDbType.NVarChar,50),
					new SqlParameter("@Number", SqlDbType.Int,4),
					new SqlParameter("@price", SqlDbType.Decimal,9),
					new SqlParameter("@PImg", SqlDbType.VarChar,50),
					new SqlParameter("@Classify", SqlDbType.Int,4),
					new SqlParameter("@LinkMan", SqlDbType.NVarChar,50),
					new SqlParameter("@TelPhone", SqlDbType.VarChar,50),
					new SqlParameter("@Mobile", SqlDbType.VarChar,50),
					new SqlParameter("@EndTime", SqlDbType.DateTime),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@PType", SqlDbType.Int,4),
					new SqlParameter("@PAddress", SqlDbType.NVarChar,50),
					new SqlParameter("@Detail", SqlDbType.NVarChar,-1),
					new SqlParameter("@UserId", SqlDbType.Int,4),
					new SqlParameter("@Status", SqlDbType.Int,4)};
            parameters[0].Value = model.Title;
            parameters[1].Value = model.PurchaseName;
            parameters[2].Value = model.Standard;
            parameters[3].Value = model.Number;
            parameters[4].Value = model.price;
            parameters[5].Value = model.PImg;
            parameters[6].Value = model.Classify;
            parameters[7].Value = model.LinkMan;
            parameters[8].Value = model.TelPhone;
            parameters[9].Value = model.Mobile;
            parameters[10].Value = model.EndTime;
            parameters[11].Value = model.CreateTime;
            parameters[12].Value = model.PType;
            parameters[13].Value = model.PAddress;
            parameters[14].Value = model.Detail;
            parameters[15].Value = model.UserId;
            parameters[16].Value = model.Status;

            int obj = Common.SqlHelper.ExecuteNonQuery(Config.SYHConnStr, CommandType.StoredProcedure, "Purchase_Info_Add", parameters);

            return Convert.ToInt32(obj);

        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(SYH178Model.Purchase_Info model)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Title", SqlDbType.NVarChar,200),
					new SqlParameter("@PurchaseName", SqlDbType.NVarChar,50),
					new SqlParameter("@Standard", SqlDbType.NVarChar,50),
					new SqlParameter("@Number", SqlDbType.Int,4),
					new SqlParameter("@price", SqlDbType.Decimal,9),
					new SqlParameter("@PImg", SqlDbType.VarChar,50),
					new SqlParameter("@Classify", SqlDbType.Int,4),
					new SqlParameter("@LinkMan", SqlDbType.NVarChar,50),
					new SqlParameter("@TelPhone", SqlDbType.VarChar,50),
					new SqlParameter("@Mobile", SqlDbType.VarChar,50),
					new SqlParameter("@EndTime", SqlDbType.DateTime),
					new SqlParameter("@CreateTime", SqlDbType.DateTime),
					new SqlParameter("@PType", SqlDbType.Int,4),
					new SqlParameter("@PAddress", SqlDbType.NVarChar,50),
					new SqlParameter("@Detail", SqlDbType.NVarChar,-1),
					new SqlParameter("@UserId", SqlDbType.Int,4),
					new SqlParameter("@Status", SqlDbType.Int,4),
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = model.Title;
            parameters[1].Value = model.PurchaseName;
            parameters[2].Value = model.Standard;
            parameters[3].Value = model.Number;
            parameters[4].Value = model.price;
            parameters[5].Value = model.PImg;
            parameters[6].Value = model.Classify;
            parameters[7].Value = model.LinkMan;
            parameters[8].Value = model.TelPhone;
            parameters[9].Value = model.Mobile;
            parameters[10].Value = model.EndTime;
            parameters[11].Value = model.CreateTime;
            parameters[12].Value = model.PType;
            parameters[13].Value = model.PAddress;
            parameters[14].Value = model.Detail;
            parameters[15].Value = model.UserId;
            parameters[16].Value = model.Status;
            parameters[17].Value = model.Id;

            int rows = Common.SqlHelper.ExecuteNonQuery(Config.SYHConnStr, CommandType.StoredProcedure, "Purchase_Info_Update", parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            int rows = Common.SqlHelper.ExecuteNonQuery(Config.SYHConnStr, CommandType.StoredProcedure, "Purchase_Info_Delete", parameters);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SYH178Model.Purchase_Info GetModel(int Id)
        {


            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
            parameters[0].Value = Id;

            SYH178Model.Purchase_Info model = new SYH178Model.Purchase_Info();
            DataSet ds = Common.SqlHelper.ExecuteDataset(Config.SYHConnStr, CommandType.StoredProcedure, "Purchase_Info_GetModel", parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SYH178Model.Purchase_Info DataRowToModel(DataRow row)
        {
            SYH178Model.Purchase_Info model = new SYH178Model.Purchase_Info();
            if (row != null)
            {
                if (row["Id"] != null && row["Id"].ToString() != "")
                {
                    model.Id = int.Parse(row["Id"].ToString());
                }
                if (row["Title"] != null)
                {
                    model.Title = row["Title"].ToString();
                }
                if (row["PurchaseName"] != null)
                {
                    model.PurchaseName = row["PurchaseName"].ToString();
                }
                if (row["Standard"] != null)
                {
                    model.Standard = row["Standard"].ToString();
                }
                if (row["Number"] != null && row["Number"].ToString() != "")
                {
                    model.Number = int.Parse(row["Number"].ToString());
                }
                if (row["price"] != null && row["price"].ToString() != "")
                {
                    model.price = decimal.Parse(row["price"].ToString());
                }
                if (row["PImg"] != null)
                {
                    model.PImg = row["PImg"].ToString();
                }
                if (row["Classify"] != null && row["Classify"].ToString() != "")
                {
                    model.Classify = int.Parse(row["Classify"].ToString());
                }
                if (row["LinkMan"] != null)
                {
                    model.LinkMan = row["LinkMan"].ToString();
                }
                if (row["TelPhone"] != null)
                {
                    model.TelPhone = row["TelPhone"].ToString();
                }
                if (row["Mobile"] != null)
                {
                    model.Mobile = row["Mobile"].ToString();
                }
                if (row["EndTime"] != null && row["EndTime"].ToString() != "")
                {
                    model.EndTime = DateTime.Parse(row["EndTime"].ToString());
                }
                if (row["CreateTime"] != null && row["CreateTime"].ToString() != "")
                {
                    model.CreateTime = DateTime.Parse(row["CreateTime"].ToString());
                }
                if (row["PType"] != null && row["PType"].ToString() != "")
                {
                    model.PType = int.Parse(row["PType"].ToString());
                }
                if (row["PAddress"] != null)
                {
                    model.PAddress = row["PAddress"].ToString();
                }
                if (row["Detail"] != null)
                {
                    model.Detail = row["Detail"].ToString();
                }
                if (row["UserId"] != null && row["UserId"].ToString() != "")
                {
                    model.UserId = int.Parse(row["UserId"].ToString());
                }
                if (row["Status"] != null && row["Status"].ToString() != "")
                {
                    model.Status = int.Parse(row["Status"].ToString());
                }
            }
            return model;
        }

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}
